import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/login'
import Home from '@/components/home'
import Welcome from '@/components/welcome'
import Users from '@/components/users'
import Rights from '@/components/rights'
import Roles from '@/components/roles'
import Cate from '@/components/goods/cate'
import { Card } from 'element-ui'

Vue.use(Router)

const router = new Router({
  routes: [
    { path: '/', redirect: '/login'},
    { path: '/login', name: 'login', component: Login },
    { path: '/home', name: 'home', component: Home, redirect:'/welcome', children:[
      {path: '/welcome', name:'welcome', component:Welcome },
      {path: '/users', name:'users', component:Users },
      {path: '/rights', name:'rights', component:Rights },
      {path: '/roles', name:'roles', component:Roles },
      {path: '/categories', name:'categories', component:Cate },
    ]},
  ]
})

router.beforeEach((to, from, next)=>{
  if (to.path == '/login') return next();

  const tokenStr = sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');
  next();
})

export default router;
